import numpy as np
import matplotlib.pyplot as plt

def hamming_distance(s1, s2):
    return sum(c1 != c2 for c1, c2 in zip(s1, s2))

# 创建4x4的二进制网格
grid_size = 4
reference = np.random.randint(2, size=(grid_size, grid_size))

# 计算汉明距离
distances = np.zeros((grid_size, grid_size))
for i in range(grid_size):
    for j in range(grid_size):
        current = np.random.randint(2, size=(grid_size, grid_size))
        distances[i, j] = hamming_distance(reference.flatten(), current.flatten())

# 绘制热力图
plt.figure(figsize=(10, 8))
plt.imshow(distances, cmap='viridis')
plt.colorbar(label='Hamming Distance')
plt.title('Hamming Distance Visualization')
plt.xlabel('X')
plt.ylabel('Y')

# 在每个格子中显示二进制值
for i in range(grid_size):
    for j in range(grid_size):
        plt.text(j, i, f"{reference[i, j]}", ha='center', va='center', color='w')

plt.show()

print(f"Example: Hamming distance between '1010' and '0110': {hamming_distance('1010', '0110')}")